import pandas as pd
import  random
from flask import Flask,request,render_template
from wordcloud import WordCloud
import time
from PIL import Image, ImageFont, ImageDraw

data = pd.read_csv("..\..\ostatic\portrait.csv\part-00000-def7b397-a7c1-4767-86cf-7aee966b6f16-c000.csv",
                   names=['phone_no', 'label', 'parent_label'], header=0)
all_user_no = data['phone_no'].to_list()
random_no= all_user_no[random.randint(0,len(all_user_no)-1)]
font_path = r'C:\\Windows\\Fonts\\STFANGSO.ttf'
app = Flask(__name__)


@app.route("/")   #路由，指向网页
def index():
    return render_template("select.html",user=all_user_no[random.randint(0,len(all_user_no)-1)],val1 =time.time())
#select.html设计好的网页内容，时间戳固定，

@app.route("/show", methods=["POST"])
def show():
    user_no= request.form.get("user_number")
    user_list = data[data['phone_no']==int(user_no)]['label'].to_list()
    string = " ".join([str(i) for i in user_list]).replace("['","").replace("']","").replace("nan","")
    word_cloud = WordCloud(font_path=font_path, width=800, height=600, mode='RGBA').generate(string)
    word_cloud.to_file("./static/images/user_img.png")

    return render_template("show.html",val2 = time.time())

if __name__ == '__main__':
    app.run(debug=True,host='127.0.0.1')



